<?php

namespace App\Http\Controller\AdminPort;

use App\Model\AuthAccountModel;
use App\Model\EmployeeModel;
use App\Service\EmployeeService;
use Hyperf\Di\Annotation\Inject;


/**
 *  企业员工管理
 * @package App\Http\Controller\AdminPort
 */
class Employee
{
    #[Inject]
    protected EmployeeService $employeeService;

    /**
     * 获取分页列表
     * @return string[]
     */
    public function getListPage()
    {
        EmployeeModel::validateColumn(
            [
                EmployeeModel::C_ACCOUNT,
                EmployeeModel::C_MOBILE,
                EmployeeModel::C_NAME,
                EmployeeModel::C_STATUS
            ]
        );

        $params = [
            EmployeeModel::C_NAME => I(EmployeeModel::C_NAME),
            EmployeeModel::C_MOBILE => I(EmployeeModel::C_MOBILE),
            EmployeeModel::C_ACCOUNT => I(EmployeeModel::C_ACCOUNT),
            EmployeeModel::C_STATUS => I(EmployeeModel::C_STATUS),
            EmployeeModel::C_COMPANY_ID => authUser()->companyId
        ];

        $data = $this->employeeService->getListByPage($params, 15);
        return getResponseData('', $data);
    }

    /**
     * 新增
     * @return array
     */
    public function create()
    {
        return $this->saveEmployee(true);
    }

    /**
     * 编辑
     * @return array
     */
    public function update()
    {
        return $this->saveEmployee(false);
    }

    /**
     * 创建和编辑用户信息
     * @param bool $isCreate
     * @return array
     */
    protected function saveEmployee(bool $isCreate = false)
    {
        $required = [
            EmployeeModel::C_NAME,
            EmployeeModel::C_ACCOUNT,
            EmployeeModel::C_IDENTITY,
            EmployeeModel::C_STATUS
        ];

        if ( ! $isCreate) {
            $required[] = EmployeeModel::C_ID;
        }

        $employee = EmployeeModel::validateColumn([
            EmployeeModel::C_MOBILE,
            EmployeeModel::C_PORTRAIT,
            EmployeeModel::R_ROLE
        ],
            $required,
            true
        );

        AuthAccountModel::validateColumn([
            AuthAccountModel::C_PASSWORD
        ]);

        $this->employeeService->save(authUser()->companyId, 1, $employee, I(AuthAccountModel::C_PASSWORD));
        return getResponseData('保存成功');
    }

    /**
     *  删除
     * @return string[]
     */
    public function delete()
    {
        EmployeeModel::validateColumn([], [EmployeeModel::C_ID]);
        $this->employeeService->delete(authUser()->companyId, I(EmployeeModel::C_ID));
        return getResponseData('删除成功');
    }
}
